/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package DAO;

import DTO.PhuLai_DTO;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 *
 * @author Phung
 */
public class PhuLai_DAO {
    
     public static String Get_IDPL() throws SQLException{
        String IDPL = null;
        MySQLDataHelper_DAO.open();
        Connection Con = MySQLDataHelper_DAO.getConnection();
        CallableStatement statement = Con.prepareCall("{call Add_CT_ID(?)}");
        statement.setString(1, "PL");
        statement.execute();
        ResultSet Rs = statement.getResultSet();
        if(Rs.next()){
            IDPL = Rs.getString(1);
        }
        return IDPL;
    }
    
    public static ArrayList<PhuLai_DTO> GetALL()
    {
        ArrayList<PhuLai_DTO> ds = new ArrayList<>();
        try {
            String sql = "Select * from PhuLai" ;
            MySQLDataHelper_DAO.open();
            ResultSet rs =  MySQLDataHelper_DAO.executeQuery(sql);
            while (rs.next()) {
                PhuLai_DTO PhuLai = new PhuLai_DTO();
                PhuLai.setID_PL(rs.getString("ID_PL"));
                PhuLai.setMaNV(rs.getString("MaNV"));
                PhuLai.setMaDT(rs.getString("MaDT"));
                PhuLai.setMaCT(rs.getString("MaCT"));
                PhuLai.setNgayDi(rs.getDate("NgayDi"));
                ds.add(PhuLai);
            }
            MySQLDataHelper_DAO.close();
       }catch (SQLException ex) {
            System.out.println(ex);
        }
        return ds;
    }
    
    public static boolean Insert(PhuLai_DTO PhuLai)
    {
        boolean flag = false;
        try {
            java.text.SimpleDateFormat fmt = new java.text.SimpleDateFormat("yyyy-MM-dd");
            String NgayDi = fmt.format(PhuLai.getNgayDi());
            String sql = "Insert PhuLai Values('"+PhuLai.getID_PL()+"','"+PhuLai.getMaNV()+"','"+PhuLai.getMaDT()+"','"+PhuLai.getMaCT()+"','"+NgayDi+"')";
            MySQLDataHelper_DAO.open();
            int kq = MySQLDataHelper_DAO.executeUpdate(sql);
            if(kq == 1)
                flag =true;
            MySQLDataHelper_DAO.close();
        }catch (Exception ex) {
            System.out.println(ex);
        }
        return flag;
    }
    
    public static boolean Update(PhuLai_DTO PhuLai)
    {
        boolean flag = false;
        try {
            java.text.SimpleDateFormat fmt = new java.text.SimpleDateFormat("yyyy-MM-dd");
            String NgayDi = fmt.format(PhuLai.getNgayDi());
            String sql = "Update PhuLai Set MaNV = '"+ PhuLai.getMaNV() +"', MaDT = '"+PhuLai.getMaDT()+"', MaCT = '"+PhuLai.getMaCT()+"', NgayDi = '"+ NgayDi+ "' where ID_PL = '"+ PhuLai.getID_PL()+"'";
            MySQLDataHelper_DAO.open();
            int kq = MySQLDataHelper_DAO.executeUpdate(sql);
            if(kq == 1)
                flag =true;
            MySQLDataHelper_DAO.close();
        }catch (Exception ex) {
            System.out.println(ex);
        }
        return flag;
    }
    
    public static boolean Delete(String ID)
    {
        boolean flag = false;
        try {
            String sql = "Delete from PhuLai where ID_PL = '"+ID+"'";
            MySQLDataHelper_DAO.open();
            int kq = MySQLDataHelper_DAO.executeUpdate(sql);
            if(kq == 1)
                flag =true;
            MySQLDataHelper_DAO.close();
        }catch (Exception ex) {
            System.out.println(ex);
        }
        return flag;
    }
}
